package com.burst.reptile.framework.interceptor;

import java.lang.reflect.Method;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.burst.reptile.common.constant.Constants;
import com.burst.reptile.common.utils.ServletUtils;
import com.burst.reptile.common.utils.ToolUtil;
import com.burst.reptile.common.utils.ip.AddressUtils;
import com.burst.reptile.common.utils.ip.IpUtils;
import com.burst.reptile.framework.interceptor.annotation.RepeatSubmit;
import com.burst.reptile.framework.web.result.Result;
import com.burst.reptile.modular.zp.entity.ZpCity;
import com.burst.reptile.modular.zp.service.IZpCityService;

import cn.hutool.core.util.StrUtil;

/**
 * 防止重复提交拦截器
 * @author wujin
 * @date: 2020年6月4日 上午9:54:55
 */
@Component
public abstract class RepeatSubmitInterceptor extends HandlerInterceptorAdapter {
	
	@Autowired
	private IZpCityService cityService;
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String citySite = null;
		Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (Constants.CITY_SITE.equals(cookie.getName())) {
                	citySite = cookie.getValue();
                	break;
                }
            }
        }
        if(StrUtil.isEmpty(citySite)) {
        	String ip = IpUtils.getIpAddr(request);
    		String address = AddressUtils.getAddressByIP(ip);
    		if(StrUtil.isNotEmpty(address)) {
    			ZpCity one = cityService.getOne(new QueryWrapper<ZpCity>().ne("parent_id", "0").like("name", address), false);
    			if(ToolUtil.isNotEmpty(one)) {
    				citySite = one.getAid();
    			}
    		}
        }
        if(StrUtil.isEmpty(citySite)) {
        	citySite = "500100";
        }
        //创建cookie
        Cookie authorization = new Cookie(Constants.CITY_SITE, citySite);
        authorization.setHttpOnly(true);
        authorization.setPath("/");
        response.addCookie(authorization);
		if (handler instanceof HandlerMethod) {
			HandlerMethod handlerMethod = (HandlerMethod) handler;
			Method method = handlerMethod.getMethod();
			RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class);
			if (annotation != null) {
				if (this.isRepeatSubmit(request)) {
					ServletUtils.renderString(response, JSONObject.toJSONString(Result.error("不允许重复提交，请稍后再试")));
					return false;
				}
			}
			return true;
		} else {
			return super.preHandle(request, response, handler);
		}
	}

	/**
	 * 验证是否重复提交由子类实现具体的防重复提交的规则
	 * 
	 * @param httpServletRequest
	 * @return
	 * @throws Exception
	 */
	public abstract boolean isRepeatSubmit(HttpServletRequest request);
}
